﻿using FireChat.Server.Session.Receiver;
using FireChat.Server.Database.Manager;
using FireChat.Share.Web.Package;
using FireChat.Share.Web.Actions;
using FireChat.Share.Web.Util;
using FireChat.Share.Web.Model.Query.Message;
using FireChat.Share.Web.Model.Enums;

namespace FireChat.Server.Session.Actions.Message;


/// <summary>
/// 好友消息
/// </summary>
internal class FriendMessageAction : ServerAction<FriendMessageQuery>
{
    public override ServertActions Type => ServertActions.FriendMessage;

    protected override ServerPackage ActionExcute(PackageReceiver receiver, FriendMessageQuery query)
    {
        var sender_id = receiver.Session.GetAccountID();
        var target_id = AccountManager.GetInstanceByName(query.Username)?.Id;

        if (target_id == null) return receiver.ReplyFailureAsync("目标账户不存在");

        
        if(!ContactsManager.IsContacts(sender_id, (int)target_id, AccountType.Account))
        {
            return receiver.ReplyFailureAsync("该目标不是你的好友");
        }

        return receiver.MessageReceived(sender_id, query.Content, (int)target_id, AccountType.Account);
    }
}
